<template>
    <section>
        <el-col :span="24">
            <el-form inline>
                <el-form-item>
                    <el-select v-model="form.supplier_id" filterable remote placeholder="输入搜索的工厂名称" size="small"
                               :remote-method="searchFactoryBtn" :loading="searchLoading" clearable>
                        <el-option v-for="(item,index) in factoryList" :label="item.factory_name" :value="item.supplier_id" :key="index" ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-input size="small" v-model.trim="form.processing_code" placeholder="加工单号" style="width: 180px;" clearable></el-input>
                </el-form-item>
                <el-form-item>
                    <el-date-picker v-model="form.date" type="month" value-format="yyyy-MM" placeholder="选择月" style="width: 140px" size="small" :clearable="false">
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="form.status" placeholder="审核状态" clearable size="small" style="width: 120px">
                        <el-option :value=0 label="未提交"></el-option>
                        <el-option :value=1 label="提交待审"></el-option>
                        <el-option :value=2 label="审核通过"></el-option>
                        <el-option :value=3 label="审核未通过"></el-option>
                        <el-option :value=4 label="取消作废"></el-option>
                        <el-option :value=5 label="已撤回"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="form.brand_code" placeholder="品牌类型" size="small" style="width: 100px">
                        <el-option value="S" label="十三余"></el-option>
                        <el-option value="Y" label="有香"></el-option>
                        <el-option value="XM" label="一弦一木"></el-option>
                        <el-option value="J" label="锦瑟"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" class="el-icon-search" @click="searchBtn" size="small" :loading="loading">查询</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <el-col :span="24">
            <el-table :data="data" style="width: 100%" :max-height="tableHeight" v-loading="loading" size="mini" @selection-change="selectBtn">
                <el-table-column type="selection" width="55" :selectable="selectable"></el-table-column>
                <el-table-column label="加工单号" prop="processing_code" width="150"></el-table-column>
                <el-table-column label="采购通知单" prop="po_id" width="150"></el-table-column>
                <el-table-column label="款号" prop="type_no" width="130"></el-table-column>
                <el-table-column label="数量" prop="totalnum" width="80"></el-table-column>
                <el-table-column label="审核状态" prop="status" width="80">
                    <template slot-scope="{ row }">
                        <el-tag size="mini" type="info" v-if="row.status==0">未提交</el-tag>
                        <el-tag size="mini" type="primary" v-else-if="row.status==1">提交待审</el-tag>
                        <el-tag size="mini" type="success" v-else-if="row.status==2">审核通过</el-tag>
                        <el-tag size="mini" type="danger" v-else-if="row.status==3">审核未通过</el-tag>
                        <el-tag size="mini" type="info" v-else-if="row.status==4">取消作废</el-tag>
                        <el-tag size="mini" type="info" v-else-if="row.status==5">已撤回</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="成衣大货单价" prop="contract_price" width="95"></el-table-column>
                <el-table-column label="成衣实付单价" prop="pay_price" width="95"></el-table-column>
                <el-table-column label="绣花大货价" prop="contract_machining_price" width="80"></el-table-column>
                <el-table-column label="绣花实付价" prop="pay_machining_price" width="80"></el-table-column>
                <el-table-column label="金额" prop="cost_amount" width="80"></el-table-column>
                <el-table-column label="加工厂" prop="factory_name" show-overflow-tooltip></el-table-column>
                <el-table-column label="操作" fixed="right" width="100">
                    <template slot-scope="{ row }">
                        <el-button type="text" size="mini" @click="linkTo(row.processing_code, 'soa_pricing_check')">提交更改</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--翻页栏-->
            <div style="margin-top: 4px;overflow: hidden;">
                <el-button size="mini" type="primary" :disabled="selectedData.length==0" @click="submitBtn">价格不变批量提交</el-button>
                <Page style="float: right" :total="total" :current="page" :page-size="limit" show-total @on-change="pageChange"></Page>
            </div>
        </el-col>
    </section>
</template>

<script>

import {cyStatisticList, cyStatisticSubmit} from "@/api/soa/cyStatistic";
import {factoryList} from "@/api/produce/factory";
import {formatDate} from "@/libs/tools";

export default {
    name: "soa_cy_statistic",
    data() {
        return {
            tableHeight: window.innerHeight - 210,
            selectIndex: 0,
            btnLoading: false,
            loading: false,
            page: 1,
            limit: 20,
            total: 0,
            data: [],
            user: {},
            nodeActions: [],

            searchLoading: false,
            form:{supplier_id: "", processing_code: "", date: formatDate(new Date().getTime(), "last_month"), status: 0, brand_code: "S"},
            factoryList: [],
            selectedData: [],
        }
    },
    watch:{
        // 监听路由参数的变化
        '$route.name': {
            immediate: true,
            handler(val) {
                if(val == "soa_cy_statistic"){
                    this.getData();
                }
            }
        },
    },
    created() {
        this.user = this.$store.getters.user;
        this.nodeActions = this.user.node_actions[this.$route.meta.id] ? this.user.node_actions[this.$route.meta.id] : [];
    },
    methods: {
        //搜索按钮
        searchBtn() {
            this.page = 1;
            this.getData();
        },
        //翻页
        pageChange(page) {
            this.page = page;
            this.getData();
        },
        //获取数据
        getData() {
            let param = {
                page: this.page,
                limit: this.limit
            };
            param = Object.assign(param, this.form);
            this.loading = true;
            cyStatisticList(param).then((res) => {
                this.loading = false;
                this.total = res.data.total;
                this.data = res.data.data;
            }).catch(() => {
                this.loading = false;
            });
        },
        searchFactoryBtn(val){
            if(val == ""){
                return;
            }
            this.getFactoryData(val);
        },
        getFactoryData(name){
            factoryList({page: 1, limit: 100, tab: 1, factory_name: name, status: 0}).then((res) =>{
                this.factoryList = res.data.data;
            })
        },
        //跳转
        linkTo(val, page){
            this.$router.push({name: page, params: {processing_code: val}});
        },
        selectable(row, index){
            if(row.pricing_examine_status === 1){
                return false;
            }
            return true;
        },
        selectBtn(val){
            this.selectedData = val;
        },
        submitBtn(){
            this.$confirm("加工单价格不变, 请再次确认", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                let list = [];
                this.selectedData.forEach(item => {
                    list.push(item.processing_code);
                });
                this.loading = true;
                cyStatisticSubmit({processing_codes: list}).then((res) => {
                    this.loading = false;
                    if (res.code === 0) {
                        this.$message.success(res.msg);
                    } else {
                        this.$message.error(res.msg);
                    }
                    this.getData();
                }).catch(() => {
                    this.loading = false;
                })
            }).catch(() =>{

            })
        },
    },
    mounted() {
        this.getFactoryData("");
        this.searchBtn();
    }
}
</script>

<style scoped>

</style>
